//
//  MyregisterViewController.m
//  parent
//
//  Created by yanzhen on 15/7/21.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "MyregisterViewController.h"
#import "ASIFormDataRequest.h"
#import "SBJsonParser.h"
#import "Utils.h"
#import "MyAuthCodeViewController.h"
#import "SVWebViewController.h"

@interface MyregisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNUm;

@property (strong, nonatomic) ASIFormDataRequest *request;

@property (weak, nonatomic) IBOutlet UILabel *userVersion;

@end

@implementation MyregisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"注册";
    [self CustomBackButton];
    _userVersion.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(version)];
    [_userVersion addGestureRecognizer:tap];
    
}

-(void)version
{
    NSString *url = [NSString stringWithFormat:@"%@%@",SERVER_URL_NEW,VERSION_HTML];
    SVWebViewController *web = [[SVWebViewController alloc]initWithURL:[NSURL URLWithString:url]];
    
    [self.navigationController pushViewController:web animated:YES];
}

- (IBAction)submit:(id)sender {
    NSString *txt = self.phoneNUm.text;
    if([txt isEqualToString:@""]){
        [self showHudComplete:@"手机号码不能为空" success:NO];
    }else if(![self isMobileNumber:txt]){
        [self showHudComplete:@"请输入正确手机号码" success:NO];
    }else{
        
        NSString *sms_register_url = [NSString stringWithFormat:@"%@%@",SERVER_URL_NEW,kSMSRegister];
        self.request = [[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:sms_register_url]];
        [self.request setUseCookiePersistence:YES];
        [self.request setPostValue:txt forKey:@"phone_num"];
        [self.request setDelegate:self];
        [self.request setDidFailSelector:@selector(requestFailed:)];
        [self.request setDidFinishSelector:@selector(requestFinished:)];
        [self.request startAsynchronous];
        self.request.hud = [[MBProgressHUD alloc] initWithView:self.view];
        [self showHud:@"正在发送" isDim:YES];
    }

}
-(void)requestFailed:(ASIHTTPRequest *)request
{
    if(request.hud){
        [request.hud hide:YES];
    }
    [self hideHud2];
}

-(void) requestFinished:(ASIHTTPRequest *) request
{
    debugLog(request.responseString);
    if(request.hud){
        [request.hud hide:YES];
    }
    [self hideHud2];
    NSString *result  = request.responseString;
    SBJsonParser *parser = [SBJsonParser new];
    NSDictionary *result_info = [parser objectWithString:result];
    NSUInteger code = [Utils defaultIntFromDic:result_info withKey:@"code"];
    if(code > 0 )
    {
        NSString *txt = self.phoneNUm.text;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:txt forKey:@"phone_no"];
        MyAuthCodeViewController *auth = [[MyAuthCodeViewController alloc]init];
        auth.phone = txt;
        [self.navigationController pushViewController:auth animated:YES];
        
    }else{
        [self showHudComplete:@"验证码短信发送失败！" success:NO];
    }
}


- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])[0-9]{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])[0-9])[0-9]{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])[0-9]{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)[0-9]{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\\\d{3})\\\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
